package com.frame.platform.menu;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import com.frame.hibernate.HibernateUtil;
import com.frame.pageform.component.tree.TreeJson;
import com.frame.pageform.component.tree.TreeNode;
import com.frame.platform.rank.Wfmenu;
import com.frame.platform.rank.Wfrankmenu;

public class MenuTreeShowJson extends TreeJson {

	private boolean rootShow = true;

	private Integer rankId;

	private List selectMenu;

	public Object getRoot() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public List query() throws Exception {
		// TODO Auto-generated method stub
		setLazy(false);// 不动态加载菜单
		List trees = new ArrayList();
		Session session = HibernateUtil.currentSession();
		if (getRankId() != null)
			selectMenu = session.createQuery(
					"select m from Wfrankmenu m where m.id.rankid=:rankid")
					.setInteger("rankid", getRankId()).list();
		if (selectMenu == null)
			selectMenu = new ArrayList();
		if (isRootShow()) {
			if(getNodeId()==null)
				setNodeId("0");
			Wfmenu menu = (Wfmenu) session.get(Wfmenu.class,
					getNodeId());
			TreeNode node = new TreeNode(menu.getId(), menu
					.getParId(), menu.getName());
			node.setExpand(true);
			getAllChildren(node, menu);
			trees.add(node);
		} else {
			if(getNodeId()==null)
				setNodeId("");
			List list = session
					.createQuery(
							"select m from Wfmenu m where m.id like ? order by m.orderid,m.id")
					.setString(0, getNodeId()+"___").list();
			for(int i=0;i<list.size();i++){
				Wfmenu menu=(Wfmenu)list.get(i);
				TreeNode node = new TreeNode(menu.getId(), menu
						.getParId(), menu.getName());
				node.setUrl(menu.getUrl());
				getAllChildren(node, menu);
				trees.add(node);
			}
		}
		HibernateUtil.closeSession();
		return trees;
	}

	private void getAllChildren(TreeNode node, Wfmenu menu) {
		String nid="";
		if(!menu.getId().equals("0"))
			nid=menu.getId();
		List children=HibernateUtil.currentSession().createQuery("select m from Wfmenu m where m.id like ? order by m.orderid,m.id").setString(0, nid+"___").list();
		if (children != null) {
			List list = node.getChildList();
			if (list == null)
				list = new ArrayList();
			for(int m=0;m<children.size();m++){
				Wfmenu menu2=(Wfmenu)children.get(m);
				TreeNode node2 = new TreeNode(menu2.getId(), menu2
						.getParId(), menu2.getName());
				node2.setOrderId(menu2.getOrderid());
				node2.setUrl(menu2.getUrl());
				for (int i = 0; i < selectMenu.size(); i++) {
					Wfrankmenu rmenu = (Wfrankmenu) selectMenu.get(i);
					if (menu2.getId().equals(rmenu.getId().getMenuid()))
						node2.setChecked(true);
				}
				list.add(node2);
				getAllChildren(node2, menu2);
			}
			node.setChildList(list);
		}
	}

	public Integer getRankId() {
		return rankId;
	}

	public void setRankId(Integer rankId) {
		this.rankId = rankId;
	}

	public List getSelectMenu() {
		return selectMenu;
	}

	public void setSelectMenu(List selectMenu) {
		this.selectMenu = selectMenu;
	}

	public boolean isRootShow() {
		return rootShow;
	}

	public void setRootShow(boolean rootShow) {
		this.rootShow = rootShow;
	}

}
